<?php

namespace App\Http\Controllers\Api;


use App\Library\OSS;
use App\Modules\Comment\Models\Comment;
use App\Modules\Common\Models\Video;
use App\Modules\Common\Models\VideoComment;
use App\Modules\Common\Models\VideoCommentReply;
use App\Modules\Common\Models\VideoThumbUpRecord;
use App\Modules\User\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Overtrue\LaravelWeChat\Facade as EasyWeChat;

class VideoController extends ApiController
{
    protected function getList(Request $request)
    {
        $this->validate($request, [
            'offset' => 'required|integer',
            'count' => 'required|integer',
        ]);

        $offset = $request->input('offset', 0);
        $count = $request->input('count', 10);
        $user = $this->getUser();

        $video = Video::query()
            ->orderBy('created_at', 'desc')
            ->offset($offset)
            ->limit($count)
            ->get();
        $record = VideoThumbUpRecord::query()
            ->whereIn('video_id', $video->pluck('id')->all())
            ->where('user_id', $user->id)
            ->get()
            ->keyBy('video_id');
        $video = $video->map(function ($item) use($record) {
            $item->has_thumb_up = is_null($record->get($item->id))? 0 : 1;
            $item->image = OSS::instance()->getUrl($item->image);
            return $item;
        });
        return $this->success($video);
    }

    public function getDetail(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer',
        ]);
        $id = $request->input('id');
        $user = $this->getUser();
        $video = Video::query()
            ->find($id);
        $video->image = OSS::instance()->getUrl($video->image);
        $record = VideoThumbUpRecord::query()
            ->where('video_id', $id)
            ->where('user_id', $user->id)
            ->first();
        if (is_null($record)) {
            $video->has_thumb_up = 0;
        } else {
            $video->has_thumb_up = 1;
        }
        return $this->success($video);
    }

    public function getComment(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer',
        ]);
        $id = $request->input('id');

        $video = Video::query()->find($id);
        if (is_null($video)) {
            return $this->fail('视频不存在');
        }

        $comment = VideoComment::query()
            ->where('video_id', $id)
            ->get();

        $reply = VideoCommentReply::query()
            ->whereIn('video_comment_id', $comment->pluck('id')->all())
            ->get();
        $ids = array_merge($comment->pluck('user_id')->all(), $reply->pluck('user_id')->all());
        $user = User::query()
            ->whereIn('id', $ids)
            ->get()
            ->keyBy('id');
        $reply = $reply->groupBy('video_comment_id');
        $re = $comment->map(function ($item) use($user, $reply) {
            $tmp = $reply->get($item->id);
            $p = [];
            if (!empty($tmp)) {
                foreach ($tmp as $value) {
                    $p[] = [
                        'nickname' => $user->get($value->user_id)->nickname,
                        'avatar' => $user->get($value->user_id)->avatar,
                        'content' => $value->content,
                        'created_at' => $value->created_at,
                    ];
                }
            }
            return [
                'id' => $item->id,
                'nickname' => $user->get($item->user_id)->nickname,
                'avatar' => $user->get($item->user_id)->avatar,
                'content' => $item->content,
                'created_at' => $item->created_at,
                'reply' => $p,
            ];
        });

        return $this->success($re);
    }

    public function comment(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer',
            'content' => 'required|string',
        ]);
        $id = $request->input('id');
        $content = $request->input('content');
        $user = $this->getUser();
        $app = EasyWeChat::miniProgram();
        $result = $app->content_security->checkText($content);
        if ($result['errcode'] != 0) {
            return $this->fail("发布内容包含敏感词");
        }
        VideoComment::query()
            ->create([
                'video_id' => $id,
                'user_id' => $user->id,
                'content' => $content,
            ]);
        Video::query()
            ->increment('comment_number');
        return $this->success();
    }

    public function reply(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer',
            'content' => 'required|integer',
        ]);
        $id = $request->input('id');
        $content = $request->input('content');
        $user = $this->getUser();
        $comment = VideoComment::query()->find($id);
        if (is_null($comment)) {
            return $this->fail('评论不存在');
        }
        $app = EasyWeChat::miniProgram();
        $result = $app->content_security->checkText($content);
        if ($result['errcode'] != 0) {
            return $this->fail("发布内容包含敏感词");
        }
        VideoCommentReply::query()
            ->create([
                'user_id' => $user->id,
                'video_id' => $comment->video_id,
                'video_comment_id' => $comment->id,
                'content' => $content,
            ]);
        return $this->success();
    }

    public function thumpUp(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer',
        ]);
        $id = $request->input('id');
        $user = $this->getUser();

        $thumpUp = VideoThumbUpRecord::query()
            ->where('video_id', $id)
            ->where('user_id', $user->id)
            ->first();
        if (is_null($thumpUp)) {
            VideoThumbUpRecord::query()
                ->create([
                    'id' => $id,
                    'video_id' => $id,
                    'user_id' => $user->id,
                ]);
            Video::query()
                ->where('id', $id)
                ->increment('thumb_up_number');
        } else {
            $thumpUp->delete();
            Video::query()
                ->where('id', $id)
                ->decrement('thumb_up_number');
        }
        return $this->success();
    }
}
